package com.example.cxz2021606127.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.appcompat.app.AppCompatActivity;

import com.example.cxz2021606127.R;
import com.example.cxz2021606127.util.ExitActivityUtil;

public class WebViewActivity extends AppCompatActivity {
    private WebView wvMainWebview;
    private ProgressDialog progressDialog;  //加载对话框

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        initView();
        progressDialog = new ProgressDialog(this);  //实例化ProgressDialog对象
        progressDialog.setMessage("玩命加载ing");


        //添加该Activity到ExitActivityUtil对象实例容器中
        ExitActivityUtil.getInstance().addActivity(this);
        WebSettings webSettings = wvMainWebview.getSettings();
        //设置 WebView 允许执行 JavaScript 脚本
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setBlockNetworkImage(false);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setLoadsImagesAutomatically(true);

        // 确保跳转到另一个网页时仍然在当前 WebView 中显示, 而不是调用浏览器打开
        wvMainWebview.setWebViewClient(new WebViewClient() {
            //网页加载时调用
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                if (!progressDialog.isShowing()) {
                    progressDialog.show();
                }

            }

            //网页结束时调用
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (progressDialog.isShowing()){
                    progressDialog.dismiss();
                }
            }
        });

        Intent intent = getIntent();
        String url = intent.getStringExtra("url");
        //加载指定网页

        wvMainWebview.loadUrl(url);
    }

    private void initView() {
        wvMainWebview = (WebView) findViewById(R.id.wv_main_webview);
    }
}